<template>
  <div class="app-container">
    <div class="search-bar">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true">
        <el-form-item label="旅行社名称" prop="deptId">
          <el-tree-select
              v-model="queryParams.deptId"
              placeholder="全部"
              :data="deptOptions"
              filterable
              clearable
              check-strictly
              :render-after-expand="false"
              style="width: 200px"
          />
        </el-form-item>
        <el-form-item label="分类名称" prop="categoryId">
          <el-tree-select
              v-model="queryParams.categoryId"
              placeholder="全部"
              :data="categoryOptions"
              filterable
              clearable
              check-strictly
              :render-after-expand="false"
              style="width: 200px"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">
            <template #icon>
              <Search/>
            </template>
            搜索
          </el-button>
          <el-button @click="handleResetQuery">
            <template #icon>
              <Refresh/>
            </template>
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never">
      <!--      <div class="mb-10px">-->
      <!--        <el-button v-hasPerm="['system:spotTravel:add']" type="success" @click="handleOpenDialog()">-->
      <!--          <template #icon>-->
      <!--            <Plus />-->
      <!--          </template>-->
      <!--          新增-->
      <!--        </el-button>-->
      <!--        <el-button-->
      <!--          v-hasPerm="['system:spotTravel:delete']"-->
      <!--          type="danger"-->
      <!--          :disabled="removeIds.length === 0"-->
      <!--          @click="handleDelete()"-->
      <!--        >-->
      <!--          <template #icon>-->
      <!--            <Delete />-->
      <!--          </template>-->
      <!--          删除-->
      <!--        </el-button>-->
      <!--      </div>-->

      <el-table
          ref="dataTableRef"
          v-loading="loading"
          :data="pageData"
          highlight-current-row
          border
          @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="50" align="center"/>
        <el-table-column type="index" label="序号" width="80" align="center"/>
        <el-table-column
            key="deptName"
            label="旅行社名称"
            prop="deptName"
            min-width="150"
            align="center"
        />
        <el-table-column
            key="categoryName"
            label="分类名称"
            prop="categoryName"
            min-width="150"
            align="center"
        />
        <el-table-column key="num" label="余票数量" prop="num" min-width="150" align="center"/>
        <el-table-column fixed="right" label="操作" align="center" width="150">
          <template #default="scope">
            <el-button
                v-hasPerm="['system:spotTravel:edit']"
                type="primary"
                size="small"
                link
                @click="handleOpenDialog(scope.row.id)"
            >
              <template #icon>
                <Edit/>
              </template>
              详情
            </el-button>
            <!--            <el-button-->
            <!--              v-hasPerm="['system:spotTravel:delete']"-->
            <!--              type="danger"-->
            <!--              size="small"-->
            <!--              link-->
            <!--              @click="handleDelete(scope.row.id)"-->
            <!--            >-->
            <!--              <template #icon>-->
            <!--                <Delete />-->
            <!--              </template>-->
            <!--              删除-->
            <!--            </el-button>-->
          </template>
        </el-table-column>
      </el-table>

      <pagination
          v-if="total > 0"
          v-model:total="total"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="handleQuery()"
      />
    </el-card>

    <!-- 旅行社数据表单弹窗 -->
    <el-dialog
        v-model="dialog.visible"
        :title="dialog.title"
        width="500px"
        @close="handleCloseDialog"
    >
      <el-form ref="dataFormRef" :model="formData" :rules="rules" label-width="100px">
        <el-form-item label="旅行社名称：" prop="deptName">
          {{ formData.deptName }}
        </el-form-item>

        <el-form-item label="分类名称：" prop="categoryName">
          {{ formData.categoryName }}
        </el-form-item>

        <el-form-item label="余票数量：" prop="num">
          {{ formData.num }}
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="handleCloseDialog()">取消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import DeptAPI from "@/api/system/dept";
import SpotTravelAPI, {SpotTravelForm, SpotTravelPageQuery, SpotTravelPageVO,} from "@/api/system/spot-travel";
import SpotCategoryAPI from "@/api/system/spot-category";

defineOptions({
  name: "SpotTravel",
  inheritAttrs: false,
});

const queryFormRef = ref(ElForm);
const dataFormRef = ref(ElForm);

const loading = ref(false);
const removeIds = ref<number[]>([]);
const total = ref(0);
const deptOptions = ref<OptionType[]>();
const categoryOptions = ref<OptionType[]>();
const queryParams = reactive<SpotTravelPageQuery>({
  pageNum: 1,
  pageSize: 10,
});

// 旅行社数据表格数据
const pageData = ref<SpotTravelPageVO[]>([]);

// 弹窗
const dialog = reactive({
  title: "",
  visible: false,
});

// 旅行社数据表单数据
const formData = reactive<SpotTravelForm>({});

// 旅行社数据表单校验规则
const rules = reactive({});

/** 查询旅行社数据 */
function handleQuery() {
  loading.value = true;
  SpotTravelAPI.getPage(queryParams)
      .then((data) => {
        pageData.value = data.list;
        total.value = data.total;
      })
      .finally(() => {
        loading.value = false;
      });
}

/** 重置旅行社数据查询 */
function handleResetQuery() {
  queryFormRef.value!.resetFields();
  queryParams.pageNum = 1;
  handleQuery();
}

/** 行复选框选中记录选中ID集合 */
function handleSelectionChange(selection: any) {
  removeIds.value = selection.map((item: any) => item.id);
}

/** 打开旅行社数据弹窗 */
function handleOpenDialog(id?: number) {
  dialog.visible = true;
  if (id) {
    dialog.title = "旅行社数据详情";
    SpotTravelAPI.getFormData(id).then((data) => {
      Object.assign(formData, data);
    });
  } else {
    dialog.title = "新增旅行社数据";
  }
}

/** 提交旅行社数据表单 */
function handleSubmit() {
  dataFormRef.value.validate((valid: any) => {
    if (valid) {
      loading.value = true;
      const id = formData.id;
      if (id) {
        SpotTravelAPI.update(id, formData)
            .then(() => {
              ElMessage.success("修改成功");
              handleCloseDialog();
              handleResetQuery();
            })
            .finally(() => (loading.value = false));
      } else {
        SpotTravelAPI.add(formData)
            .then(() => {
              ElMessage.success("新增成功");
              handleCloseDialog();
              handleResetQuery();
            })
            .finally(() => (loading.value = false));
      }
    }
  });
}

/** 关闭旅行社数据弹窗 */
function handleCloseDialog() {
  dialog.visible = false;
  dataFormRef.value.resetFields();
  dataFormRef.value.clearValidate();
  formData.id = undefined;
}

/** 删除旅行社数据 */
function handleDelete(id?: number) {
  const ids = [id || removeIds.value].join(",");
  if (!ids) {
    ElMessage.warning("请勾选删除项");
    return;
  }
  ElMessageBox.confirm("确认删除已选中的数据项?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(
      () => {
        loading.value = true;
        SpotTravelAPI.deleteByIds(ids)
            .then(() => {
              ElMessage.success("删除成功");
              handleResetQuery();
            })
            .finally(() => (loading.value = false));
      },
      () => {
        ElMessage.info("已取消删除");
      }
  );
}

if (onMounted) {
  onMounted(async () => {
    // 加载区域下拉数据源
    deptOptions.value = await DeptAPI.getOptions();
    // 加载分类下拉数据源
    categoryOptions.value = await SpotCategoryAPI.getOptions();
    handleQuery();
  });
}
</script>

<style scoped>
:deep(.el-input-number .el-input__inner) {
  text-align: left !important;
}

:deep(.el-popper) {
  max-width: 300px;
}
</style>
